require(["config"],function(){
    require(["jquery","template","header","zoom","fly","cookie"],function($,template){
        function Detail(){
            this.render();
            this.look();
        };

        Detail.prototype = {
            constructor:Detail,
            render: function(){
                $.ajax("http://rap2api.taobao.org/app/mock/86521/detailApi")
                    .done($.proxy(this.handleData,this));

            },

            handleData: function(data){
                // console.log(data);
                data = data.res_body.data;

                // console.log(data);
                var htmlimg = template("detail-template-zoom",data);
                var htmldesc = template("detail-template-desc",data);
                $(".zoombox").prepend(htmlimg);
                $(".describe").prepend(htmldesc);
                this.zoom();
                this.addListener();
            },
            zoom: function() {
				$(".middle-zoom").elevateZoom({
					gallery:'gal1', 
					cursor: 'pointer', 
					galleryActiveClass: 'active'
				});
            },

            addListener: function(){
                $(".addcart-btn").on("click",this.addToCartHandler);
                $(".reduce").on("click",this.reduceNum);
                $(".add").on("click",this.addNum);
                $(".prod_amount").on("blur",this.inputhandler);
            },

            addToCartHandler: function(){
                var currentProduct = {
                    id:$(".prod_id").text(),
                    title:$(".prod_title").text(),
                    img:$("#gal1 img:first").attr("src"),
                    sprice:$(".prod_sprice").text(),
                    lprice:$(".prod_lprice").text(),
                    amount:Number($(".prod_amount").val()),
                }
                // console.log(currentProduct);

                $.cookie.json = true;

                var products = $.cookie("cart") || [];
                var has = products.some(function(prod){
                    if(prod.id === currentProduct.id){
                        prod.amount += currentProduct.amount;
                        return true;
                    }
                    return false;
                });

                if(!has){
                    products.push(currentProduct);
                };


                $.cookie("cart",products,{expires:10,path:"/"});
                var end = $(".cart").offset();
                end.top -= $(window).scrollTop();

                var start = {top: event.pageY - $(window).scrollTop(), left: event.pageX};

                var flyer = $("#gal1 img:first").clone();
                // console.log(end);

                flyer.fly({
                    start,
                    end,
                    onEnd: function(){
                        this.destroy();
                    }
                });

                var oldnum = Number($(".amountall").text());
                var newnum = Number($(".prod_amount").val());

                $(".amountall").text(oldnum+newnum);

                return false;
            },
            
            reduceNum:function(){
                var num = Number($(".prod_amount").val());

                num--;

                if(num < 1){
                    num = 1;
                }

                $(".prod_amount").val(num);
                return false;
            },
            addNum:function(){
                var num = Number($(".prod_amount").val());

                num++;

                $(".prod_amount").val(num);
                return false;
            },

            look: function(){
                $.ajax("http://rap2api.taobao.org/app/mock/86521/look")
                    .done($.proxy(function(data){
                        data = {list:data.res_body.list};

                        var html = template("detail-template-look",data);

                        $(".look").html(html);
                    }),this);
            },
            
            inputhandler: function(){
                var text = $(this).val();
                var reg = /^[1-9]\d*$/;

                if(!reg.test(text)){
                    alert("输入数量！");
                    $(this).val(1);
                    return ;
                }
            }
        }
        
        new Detail();
    });
});